package com.meituan.catering.management.shop.biz.model.enumeration;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;

/**
 * @Author:ale
 * @Date 2022/4/22 17:07
 * @Version 1.0
 */
public interface DescribableEnum {

    int getCode();

    String getName();

    static <T extends DescribableEnum> T getByCode(Class<T> enumClazz, Integer code) {
        return Arrays.stream(enumClazz.getEnumConstants())
                .filter(candidate -> Objects.equals(candidate.getCode(), code))
                .findFirst()
                .orElse(null);
    }

    static <T extends DescribableEnum, P> T getByProperty(Class<T> enumClazz, Function<T, P> property, P value) {
        return Arrays.stream(enumClazz.getEnumConstants())
                .filter(candidate -> Objects.equals(property.apply(candidate), value))
                .findFirst()
                .orElse(null);
    }
}
